package ch.fanth.inumber.SQLite;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;



import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;



public class INumbersDbAdapter {
	
	public static final String KEY_SEARCH = "search";
	public static final String KEY_TITLE = "title";
	public static final String KEY_INUMBER = "inumber";
	public static final String KEY_BODY = "body";
	public static final String KEY_ROWID = "_id";
	public static final String KEY_CREATED = "created";

	private static final String TAG = "INumbersDbAdapter";
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;

	private static final String DATABASE_CREATE = "create table INUMBER (_id integer primary key autoincrement, "
			+ "title text not null, inumber text not null,body text not null, created text not null);";

	private static final String DATABASE_NAME = "database";
	private static final String DATABASE_TABLE = "INUMBER";
	private static final int DATABASE_VERSION = 1;

	private final Context mCtx;

	
	private static class DatabaseHelper extends SQLiteOpenHelper {

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE);
			onCreate(db);
		}
	}
	public INumbersDbAdapter(Context ctx) {
		this.mCtx = ctx;
	}

	public INumbersDbAdapter open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	public long createRecord(String title, String inumber,String body) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_TITLE, title);
		initialValues.put(KEY_INUMBER, inumber);
		initialValues.put(KEY_BODY, body);
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	
		String created = sdf.format(date);
//		Calendar calendar = Calendar.getInstance();
//		String created = calendar.get(Calendar.YEAR) + "-"
//				+ calendar.get(Calendar.MONTH) + "-"
//				+ calendar.get(Calendar.DAY_OF_MONTH) + " "
//				+ calendar.get(Calendar.HOUR_OF_DAY) + " "
//				+ calendar.get(Calendar.MINUTE) + "";
		initialValues.put(KEY_CREATED, created);
		return mDb.insert(DATABASE_TABLE, null, initialValues);
	}

	public boolean deleteRecord(long rowId) {

		return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}

	public Cursor getAllNotes() {

		return mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_TITLE,KEY_INUMBER,
				KEY_BODY, KEY_CREATED }, null, null, null, null, null,null);
	}
	public Cursor getAllBySearch(String search) {
		Cursor mCursor =

			mDb.query(true, DATABASE_TABLE, new String[] { KEY_ROWID, KEY_TITLE,KEY_INUMBER,
					KEY_BODY, KEY_CREATED }, KEY_TITLE + " like '" + search+"%'", null, null,
					null, null, null);
			if (mCursor != null) {
				mCursor.moveToFirst();
			}
			return mCursor;

	}

	public Cursor getRecord(long rowId) throws SQLException {

		Cursor mCursor =

		mDb.query(true, DATABASE_TABLE, new String[] { KEY_ROWID, KEY_TITLE,KEY_INUMBER,
				KEY_BODY, KEY_CREATED }, KEY_ROWID + "=" + rowId, null, null,
				null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}

	public boolean updateRecord(long rowId, String title,String inumber,String body) {
		ContentValues args = new ContentValues();
		args.put(KEY_TITLE, title);
		args.put(KEY_INUMBER, inumber);
		args.put(KEY_BODY, body);
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//System.out.println(sdf.format(date));
		
//		Calendar calendar = Calendar.getInstance();
//		String created = calendar.get(Calendar.YEAR) + "-"
//				+ calendar.get(Calendar.MONTH)+1 + "-"
//				+ calendar.get(Calendar.DAY_OF_MONTH)+1 + " "
//				+ calendar.get(Calendar.HOUR_OF_DAY) + " "
//				+ calendar.get(Calendar.MINUTE) + "";
		String created = sdf.format(date);
		args.put(KEY_CREATED, created);

		return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
	}
}
